% Settings
period = 6e-6;
%data = [1, 1, 0, 1, 0, 0, 1, 0, 1, 1];

% Clear screen
%clc

% constants
f=848e3; % carrier frequency

% data size
dlength = size(data,2);

% some info
%disp(['A period of ', num2str(period), 's']);
disp(['Data: ', mat2str(data), ' (', num2str(dlength), ' elements)']);

% create clock
spacing = 100;
dsignal = zeros(1,spacing*dlength);

% data signal 
for i=1:1:dlength
    dsignal((i-1)*spacing+1:(i-1)*spacing+(spacing/2))=data(i);
    dsignal((i-1)*spacing+(spacing/2 + 1):i*spacing)=1 - data(i);
end

% create plot scale
t = linspace(0, dlength/f, spacing*dlength);

% find falling edges
ix = find(diff(dsignal)<0);
edges = t(ix);
pbe = round(diff((edges - edges(1)) * f) * 10)/10; % periods between edges

plot (t, dsignal);
ylim([-0.1,1.1]);
hold on;

plot(edges,0.5,'ro');

disp(['Period between edges, starting at first edge: ', mat2str(pbe)]);
hold off;